
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragButton : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
 
    private Image img;//实例化后的对象
    Vector3 offPos;//存储按下鼠标时的图片-鼠标位置差
    // Vector3 arragedPos; //保存经过整理后的向量，用于图片移动
    /// <summary>
    /// 开始拖拽的时候
    /// </summary>
    /// <param name="eventData"></param>
    ///
    private void Awake()
    {
        offPos = new Vector3(Screen.width / -2, Screen.height / -2, 0);
    }

    private float 向上偏移 = 0f;

    public void OnBeginDrag(PointerEventData eventData)
    {
        // if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent<RectTransform>(), Input.mousePosition
        //     , eventData.enterEventCamera, out arragedPos))
        // {
        //     offPos = transform.localPosition - arragedPos;
        //     
        // }
        //
        Debug.Log("开始");
    }
    /// <summary>
    /// 拖拽中
    /// </summary>
    /// <param name="eventData"></param>
    public void OnDrag(PointerEventData eventData)
    {
        transform.localPosition = offPos + Input.mousePosition;
    }
    /// <summary>
    /// 拖拽结束，图片停留在结束位置
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData)
    {
        transform.localPosition = offPos + Input.mousePosition;
        Debug.Log("结束");
    }
}

